package com.ym.pms.controller;

import com.github.pagehelper.PageInfo;
import com.ym.pms.dao.BunkMapper;
import com.ym.pms.model.Bunk;
import com.ym.pms.model.Room;
import com.ym.pms.service.BunkService;
import com.ym.pms.utils.JSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "铺位表接口")
@RequestMapping("/bunks")
public class BunkController {

    @Autowired
    private BunkService service;

    @PreAuthorize("hasAnyRole('ADMIN','PROPERTY')")
    @ApiOperation(value = "添加铺位")
    @PostMapping
    public JSONResult add(Bunk record){
        System.out.println(record);
        int result = service.add(record);
        if (result >= 1) {
            return JSONResult.ok("添加成功");
        } else {
            return JSONResult.errorMsg("添加失败");
        }
    }

    @PreAuthorize("hasAnyRole('ADMIN','PROPERTY')")
    @ApiOperation(value = "删除铺位",notes = "根据铺位编码删除主表对应的所有内容")
    @DeleteMapping("/delete")
    @ApiImplicitParam(name = "bkCoding", value = "铺位编码", required = true, dataType = "String" )
    public JSONResult deleteByPrimaryKey(@RequestParam("bkCoding")String bkCoding){
        int result = service.delete(bkCoding);
        if (result >= 1) {
            return JSONResult.ok("删除成功");
        } else if(result == -1){
            return JSONResult.errorMsg("房间编码为空");
        } else if(result == -2){
            return JSONResult.errorMsg("未找到这条数据");
        }else{
            return JSONResult.errorMsg("删除失败");
        }
    }

    @PreAuthorize("hasAnyRole('ADMIN','PROPERTY')")
    @ApiOperation(value = "更新铺位表")
    @PostMapping("/update")
    public JSONResult update(Bunk record){
        int result = service.update(record);
        if (result >= 1) {
            return JSONResult.ok("更新成功");
        } else if(result == -1){
            return JSONResult.errorMsg("铺位编码为空");
        } else if(result == -2){
            return JSONResult.errorMsg("未找到这条数据");
        }else{
            return JSONResult.errorMsg("更新失败");
        }
    }

    @PreAuthorize("hasAnyRole('USER','ADMIN','ARCHIVIST','PROPERTY','BASIC')")
    @ApiOperation(value = "分页查询铺位表的全部信息")
    @GetMapping("/findAll")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = true, dataType = "int" ),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataType = "int" )
    })
    public JSONResult findAll(@RequestParam("pageNum")int pageNum, @RequestParam("pageSize")int pageSize){
        PageInfo pageInfo = service.findAll(pageNum,pageSize);
        return JSONResult.ok(pageInfo);
    }

    @PreAuthorize("hasAnyRole('USER','ADMIN','ARCHIVIST','PROPERTY','BASIC')")
    @ApiOperation(value = "分页模糊查询铺位表信息")
    @GetMapping("/fuzzyQuery")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = true, dataType = "int" ),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataType = "int" )
    })
    public JSONResult fuzzyQuery(Bunk bunk, @RequestParam("pageNum")int pageNum, @RequestParam("pageSize")int pageSize){
        PageInfo pageInfo = service.fuzzyQuery(bunk, pageNum, pageSize);
        return JSONResult.ok(pageInfo);
    }

    @PreAuthorize("hasAnyRole('USER','ADMIN','ARCHIVIST','PROPERTY','BASIC')")
    @ApiOperation(value = "根据coding查询铺位")
    @GetMapping("/findByCoding")
    @ApiImplicitParam(name = "bk_Coding", value = "铺位编码", required = true, dataType = "String" )
    public JSONResult findByCoding(@RequestParam("bk_Coding")String bk_Coding){
        Bunk bunk = service.findByCoding(bk_Coding);
        return JSONResult.ok(bunk);
    }

    @PreAuthorize("hasAnyRole('USER','ADMIN','ARCHIVIST','PROPERTY','BASIC')")
    @ApiOperation(value = "分页查询某房间下所有处于空置的铺位信息")
    @GetMapping("/findByStartUsing")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = true, dataType = "int" ),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataType = "int" ),
            @ApiImplicitParam(name = "rmCoding", value = "房间编号", required = true, dataType = "String" )
    })
    public JSONResult findByStartUsing(@RequestParam("rmCoding")String rmCoding, @RequestParam("pageNum")int pageNum, @RequestParam("pageSize")int pageSize){
        PageInfo pageInfo = service.findAllInRoom(rmCoding, pageNum, pageSize);
        return JSONResult.ok(pageInfo);
    }

    @PreAuthorize("hasAnyRole('USER','ADMIN')")
    @ApiOperation(value = "改变铺位状态", notes = "当用户选中铺位并提交申请后，调用此接口可以将铺位锁定，并且向上联动改变状态，" +
            "当用户申请通过后，调用此接口，可以改变铺位状态为使用中。")
    @GetMapping("/changeStatus")
    @ApiImplicitParam(name = "bkCoding", value = "铺位编号", required = true, dataType = "String" )
    public JSONResult changeStatus(@RequestParam("bkCoding")String bkCoding){
        service.changeStatus(bkCoding);
        return JSONResult.ok("修改成功");
    }
}
